package com.user.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 做缓存使用
 */
public class CatchUtil {

    private static Map<Object,Object> map = new HashMap<Object,Object>();

    /**
     * 存入缓存
     * @param key
     * @param value
     */
    public boolean putCatch(Object key, Object value) {
        if (null != map.put(key,value)){
            return true;
        }else{
            System.out.println("111");
            return false;
        }
    }

    /**
     * 存入缓存
     * @param key
     * @param value
     */
    public boolean putCatch(Object key, Object value,long time) {
        if (null != map.put(key,value)){

            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    map.remove(key);
                }
            },time);
            return true;
        }else{
            return false;
        }
    }


    /**
     * 从缓存取出值
     * @param key
     * @return
     */
    public Object getCatch(Object key){
        return map.get(key);
    }

    public boolean deleteCatch(Object key){

        if (null != map.remove(key)){
            return true;
        }else{
            return false;
        }
    }

}
